package web.servlet;

import com.alibaba.fastjson.JSONObject;
import com.rdc.example.bean.PageBean;
import com.rdc.example.bean.Result;
import com.rdc.example.bean.dto.SongDTO;
import com.rdc.example.bean.dto.UserDTO;
import com.rdc.example.service.SongService;
import com.rdc.example.service.impl.SongServiceImpl;

import javax.servlet.annotation.MultipartConfig;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;


@WebServlet("/review/*")
public class ReviewServlet extends BaseServlet {

    SongService songService=new SongServiceImpl();

    public void getSongsForReview(HttpServletRequest req, HttpServletResponse resp) throws Exception {
        System.out.println("getSongsForReview...");
        //接收 当前页码 和 每页展示条数  eg: url?currentPage=1&pageSize=5
        Integer currentPage = Integer.parseInt(req.getParameter("currentPage"));
        Integer pageSize = Integer.parseInt(req.getParameter("pageSize"));
        //在session中获取userId
        HttpSession session = req.getSession();
        UserDTO userDTO=((UserDTO) session.getAttribute("userDTO"));
        if(userDTO.getUserType()!=3){
            //权限检查 ,非管理员跳转
            resp.sendRedirect(req.getContextPath() + "/space.html");
            return;
        }
        Integer userId=userDTO.getId();
        //调用service
        PageBean<SongDTO> pageBean= songService.getSongListForCheck(userId,currentPage,pageSize);
        //json发送
        resp.setContentType("application/json;charset=utf-8");
        resp.getWriter().write(JSONObject.toJSONString(pageBean));
    }

    public void startReview(HttpServletRequest req, HttpServletResponse resp) throws Exception {
        System.out.println("startReview...");
        req.setCharacterEncoding("UTF-8");
        Integer songId = Integer.parseInt(req.getParameter("songId"));
        //在session中获取userId
        HttpSession session = req.getSession();
        UserDTO userDTO=((UserDTO) session.getAttribute("userDTO"));
        if(userDTO.getUserType()!=3){
            //权限检查 ,非管理员跳转
            resp.sendRedirect(req.getContextPath() + "/space.html");
            return;
        }
        Integer userId=userDTO.getId();
        Result result=songService.startReview(userId,songId);
        //json发送
        resp.setContentType("application/json;charset=utf-8");
        resp.getWriter().write(JSONObject.toJSONString(result));

    }
    public void passReview(HttpServletRequest req, HttpServletResponse resp) throws Exception {
        System.out.println("passReview...");
        Integer songId = Integer.parseInt(req.getParameter("songId"));
        //在session中获取userId
        HttpSession session = req.getSession();
        UserDTO userDTO=((UserDTO) session.getAttribute("userDTO"));
        if(userDTO.getUserType()!=3){
            //权限检查 ,非管理员跳转
            resp.sendRedirect(req.getContextPath() + "/space.html");
            return;
        }
        Result result=songService.passReview(songId);
        //json发送
        resp.setContentType("application/json;charset=utf-8");
        resp.getWriter().write(JSONObject.toJSONString(result));
    }

    public void rejectReview(HttpServletRequest req, HttpServletResponse resp) throws Exception {
        System.out.println("rejectReview...");
        req.setCharacterEncoding("UTF-8");
        Integer songId = Integer.parseInt(req.getParameter("songId"));
        String reason = req.getParameter("reason");
        //在session中获取userId
        HttpSession session = req.getSession();
        UserDTO userDTO=((UserDTO) session.getAttribute("userDTO"));
        if(userDTO.getUserType()!=3){
            //权限检查 ,非管理员跳转
            resp.sendRedirect(req.getContextPath() + "/space.html");
            return;
        }
        Result result=songService.rejectReview(songId,reason);
        //json发送
        resp.setContentType("application/json;charset=utf-8");
        resp.getWriter().write(JSONObject.toJSONString(result));
    }


}